package com.woniu.cinema.service.impl;

import java.util.List;
import com.woniu.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.woniu.cinema.mapper.CommentMapper;
import com.woniu.cinema.domain.Comment;
import com.woniu.cinema.service.CommentService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 影片评论Service业务层处理
 * 
 * @author yy
 * @date 2021-04-16
 */
@Service
public class CommentServiceImpl implements CommentService
{

    final static Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class);
    @Autowired
    private CommentMapper commentMapper;

    /**
     * 查询影片评论
     * 
     * @param commentId 影片评论ID
     * @return 影片评论
     */
    @Override
    public Comment selectCommentById(Long commentId)
    {
        return commentMapper.selectCommentById(commentId);
    }

    /**
     * 查询影片评论列表
     * 
     * @param comment 影片评论
     * @return 影片评论
     */
    @Override
    public List<Comment> selectCommentList(Comment comment)
    {
        return commentMapper.selectCommentList(comment);
    }

    /**
     * 新增影片评论
     * 
     * @param comment 影片评论
     * @return 结果
     */
    @Override
    public int insertComment(Comment comment)
    {
        comment.setCreateTime(DateUtils.getNowDate());
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String useridStr = request.getHeader("userid");
        logger.info("userid from zuul in insertComment of CommentServiceImpl:{}",useridStr);
        comment.setUserId(Long.valueOf(useridStr));
        return commentMapper.insertComment(comment);
    }

    /**
     * 修改影片评论
     * 
     * @param comment 影片评论
     * @return 结果
     */
    @Override
    public int updateComment(Comment comment)
    {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String useridStr = request.getHeader("userid");
        logger.info("userid from zuul in updateComment of CommentServiceImpl:{}",useridStr);
        comment.setUserId(Long.valueOf(useridStr));
        return commentMapper.updateComment(comment);
    }

    /**
     * 批量删除影片评论
     * 
     * @param commentIds 需要删除的影片评论ID
     * @return 结果
     */
    @Override
    public int deleteCommentByIds(Long[] commentIds)
    {
        return commentMapper.deleteCommentByIds(commentIds);
    }

    /**
     * 删除影片评论信息
     * 
     * @param commentId 影片评论ID
     * @return 结果
     */
    @Override
    public int deleteCommentById(Long commentId)
    {
        return commentMapper.deleteCommentById(commentId);
    }

    @Override
    public Comment queryCommentByUserAndMovie(Comment comment) {

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String useridStr = request.getHeader("userid");
        logger.info("userid from zuul in queryCommentByUserAndMovie of CommentServiceImpl:{}",useridStr);
        comment.setUserId(Long.valueOf(useridStr));

        return commentMapper.queryCommentByUserAndMovie(comment);
    }
}
